//
//  MYGCaseFilesDetailViewController.swift
//  MYGrow
//
//  Created by 王振明 on 2024/3/16.
//

import UIKit

class MYGCaseFilesDetailViewController: MYGBaseController {
    
    private var contents:[[MYGCaseSubModel]] = []
    private var array:[MYGCaseSubModel] = []
    private var  titles:[String] = []
    private var newcontents:[[[MYGCaseSubModel]]] = []
    private var newarray:[[MYGCaseSubModel]] = []
    private var  newtitles:[[String]] = []
    private var  sectionTitleArr:[String] = []
    private var  segmentSelectNum:[Int] = []

    override var hideNavigationController: Bool {
        false
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        //self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: orderButton)
        self.title = "病历夹"
        configUI()
        getCaseFileInfo()
        NotificationCenter.default.addObserver(self, selector: #selector(reloadCaseFileData), name: NSNotification.Name.init("getCaseFileData"), object: nil)
    }
    
    @objc func reloadCaseFileData() {
        getCaseFileInfo()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = false
    }
    
    private func configUI(){
        view.addSubview(bgView)
        bgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        view.addSubview(casesDetailTable)
        casesDetailTable.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalToSuperview()
        }
    }
    
    private lazy var orderButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "file_add"), for: .normal)
        btn.addTarget(self, action: #selector(clickAddBtn), for: .touchUpInside)
        return btn
    }()
    
    private lazy var bgView: UIImageView = {
        let imgV = UIImageView(image: UIImage(named: "Mine/bg"))
        return imgV
    }()
    private lazy var casesDetailTable: UITableView = {
        let cont = UITableView(frame: CGRectZero, style: .grouped)
        cont.delegate = self
        cont.dataSource = self
        cont.separatorStyle = .none
        cont.backgroundColor = .clear
        cont.showsVerticalScrollIndicator = false
        cont.tableHeaderView = headerView
        return cont
    }()

    private lazy var headerView:MYGCaseFilesDetailHeaderView = {
        let header = MYGCaseFilesDetailHeaderView()
        header.frame = CGRectMake(0, 0, screenWidth, SCALEWIDTH(300 + 35))
//        header.slectDate = { value in
//            self.array = self.contents[value]
//            self.casesDetailTable.reloadData()
//        }
        return header
    }()
    
    
    @objc
    func clickAddBtn() {
        let vc = MYGAddCaseFilesViewController()
        navigationController?.pushViewController(vc)
    }
}
extension MYGCaseFilesDetailViewController:UITableViewDataSource,UITableViewDelegate{
    func numberOfSections(in tableView: UITableView) -> Int {
        return sectionTitleArr.count
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let vi = MYGCaseFileCellHeaderView()
        vi.defautIndex = segmentSelectNum[section]
        vi.titleStr = sectionTitleArr[section]
        vi.titles = newtitles[section]
        vi.slectDate = {[self] value in
            self.newarray[section] = self.newcontents[section][value]
            segmentSelectNum[section] = value
            self.casesDetailTable.reloadData()
        }
        vi.searchBlock = {[self] in
            navigationController?.pushViewController(MYGCasefileSearchViewController())
        }
        return vi
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return SCALEWIDTH(100)
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return newarray[section].count
        return self.array.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = MYGCaseFilesDetailCell(style: .default, reuseIdentifier: "MYGCaseFilesDetailCell")
        cell.selectionStyle = .none
        cell.model = newarray[indexPath.section][indexPath.row]
//        cell.model = array[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        108
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = MYGCaseFilesViewController()
        vc.data = newarray[indexPath.section][indexPath.row]

        navigationController?.pushViewController(vc)
    }
    
}

extension MYGCaseFilesDetailViewController {
     
    func getCaseFileInfo () {
        MYGLoading.show()
        let patient_id:Int = 0
        MMYGNetwork<MYGCaseFilesApi,MYGCaseModel>.request(target: .getClassClamp(patient_id: "\(patient_id)", serche: "")) { [self] item in
            MYGLoading.hide()
            
            guard let items = item?.data,items.count > 0 else {
                return
            }
            items.forEach({ model in
                var newArr:[String] = []
                var newconArr:[[MYGCaseSubModel]] = []

                model.months.forEach { caseModel in
                    //print(caseModel.count,caseModel.month_name)
                    let arr = caseModel.lists
                    self.contents.append(arr)
                    newconArr.append(arr)
                    let title:String = "\(caseModel.month_name)(\(caseModel.count))"
                    self.titles.append(title)
                    newArr.append(title)
                }
                self.newcontents.append(newconArr)
                segmentSelectNum.append(0)
                self.newtitles.append(newArr)
                self.sectionTitleArr.append("\(model.year)(\(model.count))")
            })
            //print(contents,titles)
//            self.headerView.titles = self.titles
            self.array = self.contents.first!
            for ind in 0...self.newcontents.count - 1{
                let arr = self.newcontents[ind][0]
                self.newarray.append(arr)
            }
            self.casesDetailTable.reloadData()
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
        
        
//        MYGNetwork<MYGCaseFilesApi,MYGCaseListFileModel>.request(target: .getClassClampInfo(id: patient_id)) { item in
//            MYGLoading.hide()
//            guard let model = item else {
//                return
//            }
//            
//        } failed: { _, error in
//            MYGLoading.hide()
//            MYGHUD.showToast(text: error)
//        }
    }


}
